//
//  PickDateView.m
//  测试NativeUI
//
//  Created by YLW on 15/10/22.
//  Copyright © 2015年 YLW. All rights reserved.
//

#import "PickDateView.h"

#define kUIPickDateToolBtnHeight 40

@implementation PickDateView

- (instancetype)init
{
    if (self = [super init])
    {
        self.frame = CGRectMake(-10, 0, [UIScreen mainScreen].bounds.size.width, 162+kUIPickDateToolBtnHeight + 40);
        [self setUI];
    }
    return self;
}

- (void)setUI
{
    [self addSubview:self.toolBar];
    [self addSubview:self.datePicker];
}

- (void)disMissPickDateView:(UIButton *)btn
{
    self.disMissPickDateViewBlock(btn.tag,self.datePicker.date);
}

#pragma mark - 懒加载
- (UIDatePicker *)datePicker
{
    if (_datePicker == nil)
    {
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake( 0, kUIPickDateToolBtnHeight, self.bounds.size.width, 162)];
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        
        _datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-(50 * 365 * 24 * 60 * 60)];
        _datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:(50 * 365 * 24 * 60 * 60)];
    }
    return _datePicker;
}
+ (instancetype)sharedPickDateView
{
    static PickDateView *dateView;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateView = [[PickDateView alloc] init];
    });
    return dateView;
}
- (UIView *)toolBar
{
    if (_toolBar == nil)
    {
        _toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, kUIPickDateToolBtnHeight)];
        _toolBar.backgroundColor = [UIColor blackColor];
        
        
        [_toolBar addSubview:self.titleLabel];
        [_toolBar addSubview:self.finishBtn];
        [_toolBar addSubview:self.cancelBtn];
    }
    
    return _toolBar;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kUIPickDateToolBtnHeight)];
        _titleLabel.text = @"请选择日期";
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _titleLabel;
}

- (UIButton *)finishBtn
{
    if (_finishBtn == nil)
    {
        CGFloat btnWidth = 60;
        _finishBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - btnWidth, 0, btnWidth, kUIPickDateToolBtnHeight)];
        _finishBtn.tag = 1;
        
        [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_finishBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_finishBtn addTarget:self action:@selector(disMissPickDateView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _finishBtn;
}

- (UIButton *)cancelBtn
{
    if (_cancelBtn == nil)
    {
        CGFloat btnWidth = 60;
        _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake( 0, 0, btnWidth, kUIPickDateToolBtnHeight)];
        _cancelBtn.tag = 0;
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(disMissPickDateView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (void)setStyle:(PickDateViewStyle)style
{
    _style = style;
    
    if (style == PickDateViewStyleTime)
    {
        self.datePicker.datePickerMode = UIDatePickerModeTime;
    }
    
}

@end
